
#include <QListWidget>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QSortFilterProxyModel>
#include <QButtonGroup>
#include <QPushButton>
#include "EditLanguageView.h"
#include "LanguageModel.h"
#include "SymbolModel.h"

#include "LanguageView.h"

LanguageView::LanguageView( QWidget * parent ) : QWidget( parent )
{
    listView = new QListView();
    
    QHBoxLayout * main_layout = new QHBoxLayout( this );
    
    filteredModel = new QSortFilterProxyModel( this );
    listView->setModel( filteredModel );
    editLanguageView = new EditLanguageView();
    
    main_layout->addLayout( createLanguageElementList() );
    main_layout->addWidget( editLanguageView );
    
    connect( searchLineEdit, SIGNAL( textChanged(QString) ), filteredModel, SLOT( setFilterWildcard(QString) ) );
    connect( listView->selectionModel(), SIGNAL( currentChanged( QModelIndex, QModelIndex ) ),
        this, SLOT( currentChanged( QModelIndex, QModelIndex ) ) );
}

void LanguageView::setLanguageModel( LanguageModel * model )
{
    languageModel = model;
    editLanguageView->setModel( model );
}

void LanguageView::setSymbolModel( SymbolModel * model )
{
    symbolModel = model;
    filteredModel->setSourceModel( model );
    editLanguageView->setModel( model );
}

void LanguageView::setCurrent( const QModelIndex & index )
{
    listView->setCurrentIndex( filteredModel->mapFromSource( index ) );
}

void LanguageView::currentChanged( const QModelIndex & current, const QModelIndex & /*previous*/ )
{
    editLanguageView->setCurrent( filteredModel->mapToSource( current ) );
    emit symbolIndexChanged( filteredModel->mapToSource( current ) );
}

QLayout * LanguageView::createLanguageElementList()
{
    QVBoxLayout * list_layout = new QVBoxLayout(); 
    
    searchLineEdit = new QLineEdit;
    
    list_layout->addWidget( searchLineEdit );
    list_layout->addWidget( listView );
    
    return list_layout;
}
